package com.zboot.zbootadmin.project.system.log.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zboot.zbootadmin.common.utils.AddressUtils;
import com.zboot.zbootadmin.common.utils.DateUtils;
import com.zboot.zbootadmin.common.utils.IpUtils;
import com.zboot.zbootadmin.common.utils.ServletUtils;
import com.zboot.zbootadmin.project.system.log.entity.SysLogninIformation;
import com.zboot.zbootadmin.project.system.log.entity.dto.LoginInfoDto;
import com.zboot.zbootadmin.project.system.log.mapper.SysLoginInformationMapper;
import com.zboot.zbootadmin.project.system.log.service.SysLoginInformationService;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zyt
 * @Description:
 * @Date: Created in 11:51 2020/11/15
 */
@Service
public class SysLoginInformationServiceImpl extends ServiceImpl<SysLoginInformationMapper, SysLogninIformation> implements SysLoginInformationService {

    @Autowired
    private SysLoginInformationMapper loginInformationMapper;

    @Override
    public void insert(String msg,String status,String userName) {
        SysLogninIformation sysLogninIformation = new SysLogninIformation();
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        String address = AddressUtils.getRealAddressByIP(ip);
        sysLogninIformation.setBrowser(browser);
        sysLogninIformation.setIpaddress(ip);
        sysLogninIformation.setLoginLocation(address);
        sysLogninIformation.setLoginTime(DateUtils.getTime());
        sysLogninIformation.setOs(os);
        sysLogninIformation.setMsg(msg);
        sysLogninIformation.setStatus(status);
        sysLogninIformation.setUserName(userName);
        save(sysLogninIformation);
    }

    @Override
    public JSONObject getLoginInfo(LoginInfoDto loginDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (loginDto != null) {
            if (!"".equals(loginDto.getUserName()) && loginDto.getUserName() != null) {
                queryWrapper.like("user_name",loginDto.getUserName());
            }
            if (!"".equals(loginDto.getIpaddress()) && loginDto.getIpaddress() != null) {
                queryWrapper.like("ipaddress",loginDto.getIpaddress());
            }
            if (!"".equals(loginDto.getStatus()) && loginDto.getStatus() != null) {
                queryWrapper.like("status",loginDto.getStatus());
            }
            if (!"".equals(loginDto.getBeginTime()) && loginDto.getBeginTime()!=null && !"".equals(loginDto.getEndTime()) && loginDto.getEndTime()!=null) {
                queryWrapper.between("login_time",loginDto.getBeginTime(),loginDto.getEndTime());
            }
        }
        queryWrapper.orderByDesc("login_time");
        if (loginDto.getPageNum()!=0&&loginDto.getPageSize()!=0) {
            PageHelper.startPage(loginDto.getPageNum(),loginDto.getPageSize());
        }
        List<SysLogninIformation> result = this.list(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        jsonObject.put("total",this.count(queryWrapper));
        return jsonObject;
    }

    @Override
    public void clearInfo() {
        loginInformationMapper.clearInfo();
    }
}
